package com.kpx.app.bean;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 用于解析Json数据列表的bean
 *
 * @author CodeApe
 * @version 1.0
 * @Description TODO
 * @date 2014年12月31日
 * @Copyright: Copyright (c) 2014 Shenzhen Utoow Technology Co., Ltd. All rights reserved.
 */
public class ListBean extends BaseBean {

    /**
     * @author CodeApe
     * @version 1.0
     * @date 2014年8月8日
     */
    private static final long serialVersionUID = -2244381413323684472L;

    /** 需要进行json解析的数据类型 */
    private Class<? extends BaseBean> clazz;

    /** 记录总数 */
    private int totalCount;
    /** 当前面 */
    private int pageNow;
    /** 页大小 */
    private int totalPage;

    /** 列表数据 */
    private ArrayList<? extends BaseBean> modelList;

    public ListBean(String msgStr, Class<? extends BaseBean> clazz) throws JSONException {
        this.clazz = clazz;
        init(new JSONObject(msgStr));
    }

    public ListBean(String msgStr, Class<? extends BaseBean> clazz, String listKey) throws JSONException {
        this.clazz = clazz;
        init(msgStr, listKey);
    }

    @Override
    protected void init(JSONObject jSon) throws JSONException {
        totalCount = jSon.optInt("pageTail", 0);
        pageNow = jSon.optInt("nowPage", 0);
        totalPage = jSon.optInt("pageCount", 0);
        modelList = toModelList(jSon.optString("list"), clazz);
    }

    protected void init(String jSon, String key) throws JSONException {
        totalCount = 0;
        pageNow = 0;
        totalPage = 0;
        modelList = toModelList(jSon, clazz);
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageNow() {
        return pageNow;
    }

    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public ArrayList<? extends BaseBean> getModelList() {
        return modelList;
    }

    public void setModelList(ArrayList<? extends BaseBean> modelList) {
        this.modelList = modelList;
    }

    @Override
    public String toString() {
        return "ListBean{" +
                "totalCount=" + totalCount +
                ", pageNow=" + pageNow +
                ", totalPage=" + totalPage +
                ", modelList=" + modelList +
                '}';
    }
}
